﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BADDGUI.Controls
{
    public partial class BLCViewToolStrip : UserControl
    {
        public BindingList<KeyValuePair<string, string>> Column1, Column2;
        BindingList<string> ranges, comparisons, StatusCodes;

        #region Constructor & Load

        public BLCViewToolStrip(BLCDataGridView datagrid)
        {
            InitializeComponent();
            this.DataGrid = datagrid;
        }

        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            InitializeUI();
            InitializeEvent();
        } 

        #endregion

        #region Event

        void dataGrid_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            this.SelectedIndex = this.dataGrid.Columns[e.ColumnIndex].DisplayIndex;
        }

        #endregion

        #region Method

        public void InitializeData()
        {
            ranges = new BindingList<string> { "And","To" };
            comparisons = new BindingList<string> { "Is", "Not" };

            StatusCodes = new BindingList<string>(BLCGlobalData.BASLOG.GetADataTable("SELECT DISTINCT StatusCode FROM " + BLCGlobalObject.FrmModuleList.SelectedNode["Main Table Name"])
                                                    .AsEnumerable()
                                                    .Select(row => row.Field<string>("StatusCode")).ToArray());

            cboComparison.DataSource = comparisons;
            cboRange.DataSource = ranges;
            cboStatusCode.DataSource = StatusCodes;

            InitColumn();
        }

        public void InitializeUI()
        {
            this.Dock = DockStyle.Fill;

            cboRange.SelectedIndex = cboRange.Items.Count > 0 ? 0 : -1;
            cboComparison.SelectedIndex = cboComparison.Items.Count > 0 ? 0 : -1;
            cboStatusCode.SelectedIndex = cboStatusCode.Items.Count > 0 ? 0 : -1;
        }

        public void InitializeEvent()
        {
            this.dataGrid.ColumnHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGrid_ColumnHeaderMouseClick);
        }

        public void InitColumn()
        {
            Column1 = new BindingList<KeyValuePair<string, string>>();
            Column2 = new BindingList<KeyValuePair<string, string>>();

            foreach (DataGridViewColumn dgvCol in this.dataGrid.DisplayColumns)
            {
                string key = dgvCol.DataPropertyName;
                string value = dgvCol.HeaderText;

                Column1.Add(new KeyValuePair<string, string>(key, value));
                Column2.Add(new KeyValuePair<string, string>(key, value));
            }

            cboColumn1.DataSource = Column1;
            cboColumn1.ValueMember = "Key";
            cboColumn1.DisplayMember = "Value";

            cboColumn2.DataSource = Column2;
            cboColumn2.ValueMember = "Key";
            cboColumn2.DisplayMember = "Value";

            this.SetComboIndex();
        }

        public void SetComboIndex()
        {
            base.Refresh();
            if (this.dataGrid.SortedColumn != null && this.dataGrid.SortedColumn.Visible == true)
                this.SelectedIndex = this.dataGrid.SortedColumn.DisplayIndex;
            else this.SelectedIndex = this.dataGrid.Columns.Count > 0 ? 0 : -1;
        }

        #endregion

        #region Property

        private BLCDataGridView dataGrid;
        public BLCDataGridView DataGrid
        {
            get { return dataGrid; }
            set { dataGrid = value; }
        }

        private string _QueryFilter = "";
        public string QueryFilter
        {
            get { return _QueryFilter; }
            set { _QueryFilter = value; }
        }

        private int _SelectedIndex = -1;
        public int SelectedIndex
        {
            get { return _SelectedIndex; }
            set 
            {
                _SelectedIndex = value; 
                cboColumn1.SelectedIndex = value < cboColumn1.Items.Count ? value : -1;
                if (cboColumn2.Items.Count > 0) cboColumn2.SelectedIndex = _SelectedIndex == cboColumn2.Items.Count - 1 ? 0 : _SelectedIndex + 1;
                else cboColumn2.SelectedIndex = -1;
            }
        }

        #endregion
    }
}